import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-03-30
 * Time: 14:29
 */
public class MyLikedList {
    static class ListNode {
        public int val;
        public ListNode next;
        public ListNode prev;


        public ListNode(int val) {
            this.val = val;
        }


    }

    public ListNode head;
    public ListNode last;

    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        //要先判断是否为第一次插入
        if(head == null) {
            head = last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        //要先判断是否为第一次插入
        if(head == null) {
            head = last = node;
            return;
        }

        last.next = node;
        node.prev = last;
        last = node;
    }
    private void checkIndex(int index) throws IndexIllegal{
        if(index < 0 || index > size()) {
            throw new IndexIllegal("Index不合法: " + index);
        }
    }
    private ListNode indexNode(int index) {
        ListNode pcur = head;
        while(index > 0) {
            pcur = pcur.next;
            index--;
        }
        return pcur;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        try {
            checkIndex(index);
        }catch(IndexIllegal indexIllegal) {
            indexIllegal.printStackTrace();
        }
        if(index == 0) {
            //头插
            addFirst(data);
            return;
        }

        if(index == size()) {
            //尾插
            addLast(data);
            return;
        }


        ListNode newnode = new ListNode(data);
        ListNode pcur = indexNode(index);
        newnode.next = pcur;
        newnode.prev = pcur.prev;
        pcur.prev.next = newnode;
        pcur.prev = newnode;

    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode pcur = head;
        while(pcur != null) {
            if(pcur.val == key) {
                return true;
            }
            pcur = pcur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode pcur = head;
        while(pcur != null) {
            if(pcur.val == key) {
                if(pcur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    pcur.prev.next = pcur.next;
                    if(pcur.next == null) {
                        last = last.prev;
                    }else {
                        pcur.next.prev = pcur.prev;
                    }
                }
                return;
            }
            pcur = pcur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode pcur = head;
        while(pcur != null) {
            if(pcur.val == key) {
                if(pcur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
                    pcur.prev.next = pcur.next;
                    if(pcur.next == null) {
                        last = last.prev;
                    }else {
                        pcur.next.prev = pcur.prev;
                    }
                }
            }
            pcur = pcur.next;
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode pcur = head;
        int count = 0;
        while(pcur != null) {
            count++;
            pcur = pcur.next;
        }
        return count;
    }
    //打印节点
    public void display(){
        ListNode pcur = head;
        while(pcur != null) {
            System.out.print(pcur.val + " ");
            pcur = pcur.next;
        }
        System.out.println();
    }
    //清空链表
    public void clear(){
        ListNode pcur = head;
        while(pcur != null) {
            ListNode pNext = pcur.next;
            //pcur.val = null;如果pcur.val是引用类型就需要这也样置空
            pcur.prev = null;
            pcur.next = null;
            pcur = pNext;
        }
        head = last = null;
    }















}
